/**
 * 各类公共函数，不依赖其他库
 */
var xhTool = {
    /**
     * 根据参数名，从url获取参数值
     *
     * @param name          --要获取的参数名
     * @param unCode        --是否返回未转义数据
     * @returns string|null
     *
     * create by ck 20160324
     * modify by ck 20161208
     */
    getQuery: function(name, unCode) {
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");           //构造一个含有目标参数的正则表达式对象
        var r = window.location.search.substr(1).match(reg);            //匹配目标参数
        if(unCode){
            if (r !== null) return r[2]; return null;                      //返回参数值
        }
        if (r !== null) return unescape(r[2]); return null;                //返回参数值
    },
    /**
     * 根据参数名，获取cookie值
     *
     * @param name
     * @returns {null}
     *
     * create by ck 20170622
     */
    getCookie: function(name)
    {
        var arr, reg = new RegExp("(^| )"+name+"=([^;]*)(;|$)");
        if(arr=document.cookie.match(reg)) {
            return unescape(arr[2]);
        }
        return null;
    },
    /**
     * 格式化获取当前时间 yyyy-mm-dd hh:ii:ss
     *
     * @returns {string}
     *
     * create by ck 20160726
     */
    getNowDateTime: function(_date) {
        var date    = _date || new Date(),
            month   = date.getMonth() + 1,
            strDate = date.getDate(),
            hour    = date.getHours(),
            minutes = date.getMinutes(),
            second  = date.getSeconds();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        if (hour >= 0 && hour <= 9) {
            hour = "0" + hour;
        }
        if (minutes >= 0 && minutes <= 9) {
            minutes = "0" + minutes;
        }
        if (second >= 0 && second <= 9) {
            second = "0" + second;
        }
        return date.getFullYear()
            + '-' + month
            + '-' + strDate
            + " " + hour
            + ':' + minutes
            + ':' + second;
    },
    /**
     * 获取当前日期

     * @param addDate
     * @param _date
     * @returns {string}
     *
     * create by ck 20171023
     */
    getNowDate: function (addDate, _date) {
        var date    = _date || new Date();
        if (addDate) {
            date.setDate(date.getDate() + addDate);
        }
        var month   = date.getMonth() + 1,
            strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        return date.getFullYear()
            + '-' + month
            + '-' + strDate;
    },
    /**
     * 绑定页面回车事件，触发click事件
     *
     * @param _dom      --jquery或js对象
     *
     * create by ck 20170509
     */
    bindDocKeyDown: function(_dom) {
        document.onkeydown = function(e){
            var ev = document.all ? window.event : e;
            if(ev.keyCode === 13) {
                _dom.click();
            }
        }
    },
    /**
     * 根据路径及高度返回frame字符串
     *
     * @param _url
     * @param _params
     * @param _height
     * @returns {string}
     *
     * create by ck 20170809
     * modify by ck 20170817    可传入参数
     * modify by ck 20171117    更改url判断
     */
    getFrameString: function(_url, _params, _height){
        _url = _params ? _url + '?' + xhTool.parseParam(_params) : _url;
        if (_url.substr(0,1) === '/') _url = _url.substr(1);
        // return `<iframe frameborder="0" src="${baseUrl}${_url}" style="width:100%; height: ${_height}px;"></iframe>`;
        return '<iframe frameborder="0" src="' + baseUrl + _url + '" style="width:100%; height: ' + _height + 'px;"></iframe>';
    },
    /**
     * 组合参数
     *
     * @param param
     * @returns {string}
     *
     * create by ck 20180817
     */
    parseParam: function(param) {
        var paramStr = '';
        $.each(param, function (key, value) {
            paramStr += '&' + key + '=' + encodeURIComponent(value);
        });
        return paramStr.substr(1);
    },
    /**
     * 清除数组中无用数据
     *
     * @param arr
     * @returns {*}
     *
     * create by ck 20170927
     */
    clearNullArr: function(arr){
        for (var i = 0, len = arr.length; i < len; i++) {
            if (!arr[i] || arr[i] == '' || arr[i] === undefined) {
                arr.splice(i, 1);
                len--;
                i--;
            }
        }
        return arr;
    },
    /**
     *  获取选中文字
     *
     *  @returns {*}
     *
     *  create by ck 20180511
     */
    getSelectText: function () {
        if (window.getSelection) {
            return window.getSelection().toString();
        } else if (document.getSelection) {
            return document.getSelection();
        } else if (document.selection) {
            return document.selection.createRange().text;
        }else{
            return "";
        }
    }
};
